home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perltrap.1 < prev    next >
Text File  |  1995-07-25  |  17KB  |  463 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perltrap - Perl traps for the unwary
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           The biggest trap of all is forgetting to use the ----wwww switch;
  13.           see the _p_e_r_l_r_u_n manpage.  Making your entire program
  14.           runnable under
  15.  
  16.               use strict;
  17.  
  18.           can help make your program more bullet-proof, but sometimes
  19.           it's too annoying for quick throw-away programs.
  20.  
  21.           AAAAwwwwkkkk TTTTrrrraaaappppssss
  22.  
  23.           Accustomed aaaawwwwkkkk users should take special note of the
  24.           following:
  25.  
  26.           o+   The English module, loaded via
  27.  
  28.                   use English;
  29.  
  30.               allows you to refer to special variables (like $RS) as
  31.               though they were in aaaawwwwkkkk; see the _p_e_r_l_v_a_r manpage for
  32.               details.
  33.  
  34.           o+   Semicolons are required after all simple statements in
  35.               Perl (except at the end of a block).  Newline is not a
  36.               statement delimiter.
  37.  
  38.           o+   Curly brackets are required on ifs and whiles.
  39.  
  40.           o+   Variables begin with "$" or "@" in Perl.
  41.  
  42.           o+   Arrays index from 0.  Likewise string positions in
  43.               _s_u_b_s_t_r() and _i_n_d_e_x().
  44.  
  45.           o+   You have to decide whether your array has numeric or
  46.               string indices.
  47.  
  48.           o+   Associative array values do not spring into existence
  49.               upon mere reference.
  50.  
  51.           o+   You have to decide whether you want to use string or
  52.               numeric comparisons.
  53.  
  54.           o+   Reading an input line does not split it for you.  You
  55.               get to split it yourself to an array.  And _s_p_l_i_t()
  56.               operator has different arguments.
  57.  
  58.           o+   The current input line is normally in $_, not $0.  It
  59.               generally does not have the newline stripped.  ($0 is
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  71.  
  72.  
  73.  
  74.               the name of the program executed.)  See the _p_e_r_l_v_a_r
  75.               manpage.
  76.  
  77.           o+   $<_d_i_g_i_t> does not refer to fields--it refers to
  78.               substrings matched by the last match pattern.
  79.  
  80.           o+   The _p_r_i_n_t() statement does not add field and record
  81.               separators unless you set $, and $..  You can set $OFS
  82.               and $ORS if you're using the English module.
  83.  
  84.           o+   You must open your files before you print to them.
  85.  
  86.           o+   The range operator is "..", not comma.  The comma
  87.               operator works as in C.
  88.  
  89.           o+   The match operator is "=~", not "~".  ("~" is the one's
  90.               complement operator, as in C.)
  91.  
  92.           o+   The exponentiation operator is "**", not "^".  "^" is
  93.               the XOR operator, as in C.  (You know, one could get the
  94.               feeling that aaaawwwwkkkk is basically incompatible with C.)
  95.  
  96.           o+   The concatenation operator is ".", not the null string.
  97.               (Using the null string would render /pat/ /pat/
  98.               unparsable, since the third slash would be interpreted
  99.               as a division operator--the tokener is in fact slightly
  100.               context sensitive for operators like "/", "?", and ">".
  101.               And in fact, "." itself can be the beginning of a
  102.               number.)
  103.  
  104.           o+   The next, exit, and continue keywords work differently.
  105.  
  106.           o+   The following variables work differently:
  107.  
  108.                     Awk       Perl
  109.                     ARGC      $#ARGV or scalar @ARGV
  110.                     ARGV[0]   $0
  111.                     FILENAME  $ARGV
  112.                     FNR       $. - something
  113.                     FS        (whatever you like)
  114.                     NF        $#Fld, or some such
  115.                     NR        $.
  116.                     OFMT      $#
  117.                     OFS       $,
  118.                     ORS       $\
  119.                     RLENGTH   length($&)
  120.                     RS        $/
  121.                     RSTART    length($`)
  122.                     SUBSEP    $;
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  137.  
  138.  
  139.  
  140.           o+   You cannot set $RS to a pattern, only a string.
  141.  
  142.           o+   When in doubt, run the aaaawwwwkkkk construct through aaaa2222pppp and see
  143.               what it gives you.
  144.  
  145.           CCCC TTTTrrrraaaappppssss
  146.  
  147.           Cerebral C programmers should take note of the following:
  148.  
  149.           o+   Curly brackets are required on if's and while's.
  150.  
  151.           o+   You must use elsif rather than else if.
  152.  
  153.           o+   The break and continue keywords from C become in Perl
  154.               last and next, respectively.  Unlike in C, these do _N_O_T
  155.               work within a do { } while construct.
  156.  
  157.           o+   There's no switch statement.  (But it's easy to build
  158.               one on the fly.)
  159.  
  160.           o+   Variables begin with "$" or "@" in Perl.
  161.  
  162.           o+   _p_r_i_n_t_f() does not implement the "*" format for
  163.               interpolating field widths, but it's trivial to use
  164.               interpolation of double-quoted strings to achieve the
  165.               same effect.
  166.  
  167.           o+   Comments begin with "#", not "/*".
  168.  
  169.           o+   You can't take the address of anything, although a
  170.               similar operator in Perl 5 is the backslash, which
  171.               creates a reference.
  172.  
  173.           o+   ARGV must be capitalized.
  174.  
  175.           o+   System calls such as _l_i_n_k(), _u_n_l_i_n_k(), _r_e_n_a_m_e(), etc.
  176.               return nonzero for success, not 0.
  177.  
  178.           o+   Signal handlers deal with signal names, not numbers.
  179.               Use kill -l to find their names on your system.
  180.  
  181.           SSSSeeeedddd TTTTrrrraaaappppssss
  182.  
  183.           Seasoned sssseeeedddd programmers should take note of the following:
  184.  
  185.           o+   Backreferences in substitutions use "$" rather than "\".
  186.  
  187.           o+   The pattern matching metacharacters "(", ")", and "|" do
  188.               not have backslashes in front.
  189.  
  190.           o+   The range operator is ..., rather than comma.
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  203.  
  204.  
  205.  
  206.           SSSShhhheeeellllllll TTTTrrrraaaappppssss
  207.  
  208.           Sharp shell programmers should take note of the following:
  209.  
  210.           o+   The backtick operator does variable interpretation
  211.               without regard to the presence of single quotes in the
  212.               command.
  213.  
  214.           o+   The backtick operator does no translation of the return
  215.               value, unlike ccccsssshhhh.
  216.  
  217.           o+   Shells (especially ccccsssshhhh) do several levels of
  218.               substitution on each command line.  Perl does
  219.               substitution only in certain constructs such as double
  220.               quotes, backticks, angle brackets, and search patterns.
  221.  
  222.           o+   Shells interpret scripts a little bit at a time.  Perl
  223.               compiles the entire program before executing it (except
  224.               for BEGIN blocks, which execute at compile time).
  225.  
  226.           o+   The arguments are available via @ARGV, not $1, $2, etc.
  227.  
  228.           o+   The environment is not automatically made available as
  229.               separate scalar variables.
  230.  
  231.           PPPPeeeerrrrllll TTTTrrrraaaappppssss
  232.  
  233.           Practicing Perl Programmers should take note of the
  234.           following:
  235.  
  236.           o+   Remember that many operations behave differently in a
  237.               list context than they do in a scalar one.  See the
  238.               _p_e_r_l_d_a_t_a manpage for details.
  239.  
  240.           o+   Avoid barewords if you can, especially all lower-case
  241.               ones.  You can't tell just by looking at it whether a
  242.               bareword is a function or a string.  By using quotes on
  243.               strings and parens on function calls, you won't ever get
  244.               them confused.
  245.  
  246.           o+   You cannot discern from mere inspection which built-ins
  247.               are unary operators (like _c_h_o_p() and _c_h_d_i_r()) and which
  248.               are list operators (like _p_r_i_n_t() and _u_n_l_i_n_k()).  (User-
  249.               defined subroutines can oooonnnnllllyyyy be list operators, never
  250.               unary ones.)  See the _p_e_r_l_o_p manpage.
  251.  
  252.           o+   People have a hard type remembering that some functions
  253.               default to $_, or @ARGV, or whatever, but that others
  254.               which you might expect to do not.
  255.  
  256.           o+   Remember not to use "=" when you need "=~"; these two
  257.               constructs are quite different:
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  269.  
  270.  
  271.  
  272.                   $x =  /foo/;
  273.                   $x =~ /foo/;
  274.  
  275.  
  276.           o+   The do {} construct isn't a real loop that you can use
  277.               loop control on.
  278.  
  279.           o+   Use _m_y() for local variables whenever you can get away
  280.               with it (but see the _p_e_r_l_f_o_r_m manpage for where you
  281.               can't). Using _l_o_c_a_l() actually gives a local value to a
  282.               global variable, which leaves you open to unforeseen
  283.               side-effects of dynamic scoping.
  284.  
  285.           PPPPeeeerrrrllll4444 TTTTrrrraaaappppssss
  286.  
  287.           Penitent Perl 4 Programmers should take note of the
  288.           following incompatible changes that occurred between release
  289.           4 and release 5:
  290.  
  291.           o+   @ now always interpolates an array in double-quotish
  292.               strings.  Some programs may now need to use backslash to
  293.               protect any @ that shouldn't interpolate.
  294.  
  295.      like subroutine calls if a subroutine by that name is defined
  296.      before the compiler sees them. For example:
  297.           o+ Barewords that used to look like strings to Perl will now look
  298.  
  299.                   sub SeeYa { die "Hasta la vista, baby!" }
  300.                   $SIG{QUIT} = SeeYa;
  301.  
  302.               In Perl 4, that set the signal handler; in Perl 5, it
  303.               actually calls the function!  You may use the ----wwww switch
  304.               to find such places.
  305.  
  306.           o+   Symbols starting with _ are no longer forced into
  307.               package main, except for $_ itself (and @_, etc.).
  308.  
  309.           o+   s'$lhs'$rhs' now does no interpolation on either side.
  310.               It used to interpolate $lhs but not $rhs.
  311.  
  312.           o+   The second and third arguments of _s_p_l_i_c_e() are now
  313.               evaluated in scalar context (as the book says) rather
  314.               than list context.
  315.  
  316.           o+   These are now semantic errors because of precedence:
  317.  
  318.                   shift @list + 20;
  319.                   $n = keys %map + 20;
  320.  
  321.               Because if that were to work, then this couldn't:
  322.  
  323.                   sleep $dormancy + 20;
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  335.  
  336.  
  337.  
  338.           o+   open FOO || die is now incorrect.  You need parens
  339.               around the filehandle.  While temporarily supported,
  340.               using such a construct will generate a non-fatal (but
  341.               non-suppressible) warning.
  342.  
  343.           o+   The elements of argument lists for formats are now
  344.               evaluated in list context.  This means you can
  345.               interpolate list values now.
  346.  
  347.           o+   You can't do a goto into a block that is optimized away.
  348.               Darn.
  349.  
  350.           o+   It is no longer syntactically legal to use whitespace as
  351.               the name of a variable, or as a delimiter for any kind
  352.               of quote construct.  Double darn.
  353.  
  354.           o+   The _c_a_l_l_e_r() function now returns a false value in a
  355.               scalar context if there is no caller.  This lets library
  356.               files determine if they're being required.
  357.  
  358.           o+   m//g now attaches its state to the searched string
  359.               rather than the regular expression.
  360.  
  361.           o+   reverse is no longer allowed as the name of a sort
  362.               subroutine.
  363.  
  364.           o+   ttttaaaaiiiinnnnttttppppeeeerrrrllll is no longer a separate executable.  There is
  365.               now a ----TTTT switch to turn on tainting when it isn't turned
  366.               on automatically.
  367.  
  368.           o+   Double-quoted strings may no longer end with an
  369.               unescaped $ or @.
  370.  
  371.           o+   The archaic while/if BLOCK BLOCK syntax is no longer
  372.               supported.
  373.  
  374.           o+   Negative array subscripts now count from the end of the
  375.               array.
  376.  
  377.           o+   The comma operator in a scalar context is now guaranteed
  378.               to give a scalar context to its arguments.
  379.  
  380.           o+   The ** operator now binds more tightly than unary minus.
  381.               It was documented to work this way before, but didn't.
  382.  
  383.           o+   Setting $#array lower now discards array elements.
  384.  
  385.           o+   _d_e_l_e_t_e() is not guaranteed to return the old value for
  386.               _t_i_e()d arrays, since this capability may be onerous for
  387.               some modules to implement.
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  401.  
  402.  
  403.  
  404.           o+   Some error messages will be different.
  405.  
  406.           o+   Some bugs may have been inadvertently removed.
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.